//
//  LBXDAVServer.m
//  WebServerTools
//
//  Created by 夏利兵 on 2020/9/24.
//  Copyright © 2020 com.lbx. All rights reserved.
//

#import "LBXDAVServer.h"

#import <GCDWebDAVServer.h>

#import <ifaddrs.h>
#import <arpa/inet.h>

@interface LBXDAVServer()

//DAV服务器，支持trasmit This lets users upload, download, delete files and create directories from a directory inside your iOS app's sandbox using any WebDAV client like Transmit (Mac), ForkLift (Mac) or CyberDuck (Mac / Windows).
//transmit 协议选择 WebDAV
@property (nonatomic, strong) GCDWebDAVServer *webDAVServer;
@end


@implementation LBXDAVServer


+ (instancetype)sharedManager
{
    static LBXDAVServer* _sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedInstance = [[LBXDAVServer alloc] init];
        _sharedInstance.port = 60002;
        
    });
    
    return _sharedInstance;
}

- (BOOL)start
{
    
    if (_webDAVServer) {
        return YES;
    }
    //理论1024 之下的没有权限
    NSUInteger port = self.port;
    
    //可上传、删除、下载，修改，类似ftp
    
    self.webDAVServer = [[GCDWebDAVServer alloc] initWithUploadDirectory:self.fileDir];
    
    
    if (![_webDAVServer startWithPort:self.port bonjourName:nil])
    {
        self.errMsg = [NSString stringWithFormat:@"启动失败 端口号：%lu (注意是不是端口被占用了)",(unsigned long)port];
        self.webDAVServer = nil;
        return NO;
    }
    
    self.published = YES;
    return YES;
}

- (void)stop
{
    if (_webDAVServer) {
        [_webDAVServer stop];
        self.webDAVServer = nil;
        self.published = NO;
    }
}



- (NSString*)visitUrl
{
    NSString *visitUrl = [NSString stringWithFormat:@"%@:%lu (ftp工具协议选择WebDAV)",[self deviceIPAdress],(unsigned long)self.port];
    
    return visitUrl;
}

- (NSString *)deviceIPAdress {
    
    NSString *address = @"";
    
    struct ifaddrs *interfaces = NULL;
    
    struct ifaddrs *temp_addr = NULL;
    
    int success = 0;
    
    success = getifaddrs(&interfaces);
    
    if (success == 0) { // 0 表示获取成功
        
        temp_addr = interfaces;
        
        while (temp_addr != NULL) {
            
            if( temp_addr->ifa_addr->sa_family == AF_INET) {
                
                // Check if interface is en0 which is the wifi connection on the iPhone
                //                NSLog(@"%@",[NSString stringWithUTF8String:temp_addr->ifa_name]);
                //                 NSString* str = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];
                //                NSLog(@"%@",str);
                
                if ([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"] || [[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en1"] ) {
                    
                    // Get NSString from C String
                    address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];
                    
                }
            }
            
            temp_addr = temp_addr->ifa_next;
        }
    }
    
    freeifaddrs(interfaces);
    
    return address;
    
}

@end

